package net.realsee.common.exception.mes;

import cn.hutool.core.util.StrUtil;

/**
 * Mes任务创建、执行、取消、修改等异常
 *
 * @author 孙正华
 * @since 2023/8/24 13:52
 */
public class GclMesTaskException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private final CodeEnum codeEnum;
    private final String errorMessage;

    public GclMesTaskException(CodeEnum codeEnum) {
        this.codeEnum = codeEnum;
        this.errorMessage = StrUtil.EMPTY;
    }

    public GclMesTaskException(CodeEnum codeEnum, String errorMessage) {
        this.codeEnum = codeEnum;
        this.errorMessage = errorMessage;
    }

    public CodeEnum getCodeEnum() {
        return codeEnum;
    }

    public String getErrorMessage() {
        return StrUtil.isNotBlank(errorMessage) ? errorMessage : codeEnum.getMessage();
    }

    public enum CodeEnum {

        START_MAPMARK_NOT_EXIST("E001", "任务开始地标不存在"),
        END_MAPMARK_NOT_EXIST("E002", "任务目的地标不存在"),
        TASK_REPEAT("E003", "任务重复下发"),
        QR_SCAN_ERR("E004", "开始地标获取二维码失败"),
        AVOIDING("E005", "目的地标避障无法进入"),
        TASK_NOT_EXIST("E006", "任务不在队列中"),
        TASK_EXECUTING("E007", "任务正在执行中，不可取消"),
        TASK_COMPLETED("E008", "任务已执行完成"),
        TASK_TEMP_NOT_EXIST("E009", "任务模板不存在"),
        NETWORK_ERR("E010", "RCS网络异常"),
        UNKNOWN_ERR("E999", "系统异常"),

        ER11("ER11", "背货未识别"),
        ER14("ER14", "货码无法识别"),
        ER21("ER21", "平台下发指令错误"),
        ER24("ER24", "货架位置偏移"),

        TASK_CANCEL("NG001", "WCS/RCS任务主动取消"),
        ;

        private final String code;
        private final String message;

        CodeEnum(String code, String message) {
            this.code = code;
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }
}
